import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:intl/intl.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/views/module_notice/notice_detail/state.dart';
import 'package:timeline_tile/timeline_tile.dart';

import '../../../../constants.dart';
import '../../../../models/notice_v2_entity.dart';

class HistoryInfoWidget extends StatelessWidget {
  const HistoryInfoWidget({
    Key? key,
    required this.data,
  }) : super(key: key);

  final NoticeDetailState data;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      padding: EdgeInsets.all(kSpaceMedium),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(kRadiusMedium),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: List.generate(data.noticeData.value.history!.length, (index) {
          return _buildHistoryItem(
            index: index,
            item: data.noticeData.value.history![index],
          );
        }),
      ),
    );
  }

  Widget _buildHistoryItem({
    required int index,
    required NoticeV2History item,
  }) {
    return TimelineTile(
      alignment: TimelineAlign.start,
      isFirst: index == 0,
      isLast: index == data.noticeData.value.history!.length - 1,
      beforeLineStyle: LineStyle(
        color: AppColor.colorLineGrey,
        thickness: 0.5.w,
      ),
      afterLineStyle: LineStyle(
        color: AppColor.colorLineGrey,
        thickness: 0.5.w,
      ),
      indicatorStyle: IndicatorStyle(
          indicatorXY: 0.2,
          indicator: SvgPicture.asset(
            'assets/icons/ic_timeline_tip.svg',
            width: 12.w,
            height: 12.w,
          )),
      endChild: Padding(
        padding: EdgeInsets.only(left: kSpaceSmall, bottom: kSpaceSmall, top: kSpaceSmall),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  DateFormat('yyyy.MM.dd HH:mm').format(
                      DateTime.fromMillisecondsSinceEpoch(item.createdAt!)),
                  style: TextStyle(
                    color: AppColor.colorFontBlack,
                    fontSize: kFontSizeMedium,
                  ),
                ),
                Text(
                  item.name ?? '',
                  style: TextStyle(
                    color: AppColor.colorFontBlack,
                    fontSize: kFontSizeMedium,
                  ),
                ),
              ],
            ),
            SizedBox(height: kSpaceSmall),
            Container(
              width: double.infinity,
              padding: EdgeInsets.symmetric(horizontal: kSpaceSmall, vertical: kSpaceMini),
              decoration: BoxDecoration(
                color: AppColor.colorRectangleGrey,
                borderRadius: BorderRadius.circular(kRadiusMedium),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    data.historyType[item.type]!.$1,
                    style: TextStyle(
                      fontSize: kFontSizeSmall,
                      color: data.historyType[item.type]!.$2,
                    ),
                  ),
                  SizedBox(height: kSpaceMini),
                  Text(
                    '备注: ${item.remark ?? ''}',
                    style: TextStyle(
                      fontSize: kFontSizeSmall,
                      color: Colors.grey,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
